package org.example.test_qlexpress.function;

import com.ql.util.express.Operator;

import java.math.BigDecimal;
import java.math.BigInteger;

public class AbsFunction extends Operator {
    public Object executeInner(Object[] objects) throws Exception {
        if (objects == null || objects.length == 0) {
            throw new RuntimeException("没有传入任何参数。");
        }
        Object object = objects[0];
        if (object == null) {
            throw new RuntimeException("参数不能为null。");
        }
        if (object instanceof Byte) {
            Byte v = (Byte) object;
            if (v < 0) {
                return -v;
            }
            return v;
        }
        if (object instanceof Short) {
            Short v = (Short) object;
            if (v < 0) {
                return -v;
            }
            return v;
        }
        if (object instanceof Integer) {
            Integer v = (Integer) object;
            if (v < 0) {
                return -v;
            }
            return v;
        }
        if (object instanceof Long) {
            Long v = (Long) object;
            if (v < 0) {
                return -v;
            }
            return v;
        }
        if (object instanceof Float) {
            Float v = (Float) object;
            if (v < 0) {
                return -v;
            }
            return v;
        }
        if (object instanceof Double) {
            Double v = (Double) object;
            if (v < 0) {
                return -v;
            }
            return v;
        }
        if (object instanceof BigInteger) {
            BigInteger v = (BigInteger) object;
            return v.abs();
        }
        if (object instanceof BigDecimal) {
            BigDecimal v = (BigDecimal) object;
            return v.abs();
        }
        throw new RuntimeException("不支持该数据类型：" + object);
    }
}
